package com.jxt.myjxtmanager.db.dao;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;

import com.jxt.myjxtmanager.db.DaoSession;
import com.jxt.myjxtmanager.db.Subject;

import de.greenrobot.dao.AbstractDao;
import de.greenrobot.dao.Property;
import de.greenrobot.dao.internal.DaoConfig;

/**
 * Created by qm on 2017/3/7 0007.
 */
public class SubjectDao extends AbstractDao<Subject, Long> {
    public static final String TABLENAME = "SUBJECT";

    /**
     * Properties of entity Message_task.<br/>
     * Can be used for QueryBuilder and for referencing column names.
     */
    public static class Properties {
        public final static Property Id = new Property(0, Long.class, "id", true, "_id");
        public final static Property SubName = new Property(1, String.class, "subName", false, "SUBNAME");
        public final static Property SubScoreA = new Property(2, String.class, "subScoreA", false, "SUBSCOREA");
        public final static Property SubScoreB = new Property(3, String.class, "subScoreB", false, "SUBSCOREB");
        public final static Property SubScoreC = new Property(4, String.class, "subScoreC", false, "SUBSCOREC");
        public final static Property SubScoreD = new Property(5, String.class, "subScoreD", false, "SUBSCORED");
    }

    public SubjectDao(DaoConfig config) {
        super(config);
    }

    public SubjectDao(DaoConfig config, DaoSession daoSession) {
        super(config, daoSession);
    }

    /**
     * Creates the underlying database table.
     */
    public static void createTable(SQLiteDatabase db, boolean ifNotExists) {
        String constraint = ifNotExists ? "IF NOT EXISTS " : "";
        db.execSQL("CREATE TABLE " + constraint + "'SUBJECT' (" + //
                "'_id' INTEGER PRIMARY KEY ," + // 0: id
                "'SUBNAME' TEXT NOT NULL ," + // 1: send_time
                "'SUBSCOREA' TEXT," + // 3: content
                "'SUBSCOREB' TEXT," + // 5: title
                "'SUBSCOREC' TEXT," + // 5: title
                "'SUBSCORED' TEXT);"); // 7: un_read_message
    }

    /**
     * Drops the underlying database table.
     */
    public static void dropTable(SQLiteDatabase db, boolean ifExists) {
        String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "'SUBJECT'";
        db.execSQL(sql);
    }

    /**
     * @inheritdoc
     */
    @Override
    protected void bindValues(SQLiteStatement stmt, Subject entity) {
        stmt.clearBindings();

        Long id = entity.getId();
        if (id != null) {
            stmt.bindLong(1, id);
        }
        stmt.bindString(2, entity.getSubName());
        stmt.bindString(3, entity.getSubScoreA());

        String subScoreB = entity.getSubScoreB();
        if (subScoreB != null) {
            stmt.bindString(4, subScoreB);
        }

        stmt.bindString(5, entity.getSubScoreC());

        String subScoreD = entity.getSubScoreD();
        if (subScoreD != null) {
            stmt.bindString(6, subScoreD);
        }
    }

    /**
     * @inheritdoc
     */
    @Override
    public Long readKey(Cursor cursor, int offset) {
        return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0);
    }

    /**
     * @inheritdoc
     */
    @Override
    public Subject readEntity(Cursor cursor, int offset) {
        Subject entity = new Subject(
                cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // id
                cursor.getString(offset + 1), // send_time
                cursor.getString(offset + 2), // status
                cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3), // content
                cursor.getString(offset + 4), // msg_type
                cursor.isNull(offset + 5) ? null : cursor.getString(offset + 5) // title
        );
        return entity;
    }

    /**
     * @inheritdoc
     */
    @Override
    public void readEntity(Cursor cursor, Subject entity, int offset) {
        entity.setId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0));
        entity.setSubName(cursor.getString(offset + 1));
        entity.setSubScoreA(cursor.getString(offset + 2));
        entity.setSubScoreB(cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3));
        entity.setSubScoreC(cursor.getString(offset + 4));
        entity.setSubScoreD(cursor.isNull(offset + 5) ? null : cursor.getString(offset + 5));
    }

    /**
     * @inheritdoc
     */
    @Override
    protected Long updateKeyAfterInsert(Subject entity, long rowId) {
        entity.setId(rowId);
        return rowId;
    }

    /**
     * @inheritdoc
     */
    @Override
    public Long getKey(Subject entity) {
        if (entity != null) {
            return entity.getId();
        } else {
            return null;
        }
    }

    /**
     * @inheritdoc
     */
    @Override
    protected boolean isEntityUpdateable() {
        return true;
    }

}
